unit FormCadastroGondolaUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.Grids,
  Vcl.DBGrids, Data.DB, Bde.DBTables;

type
  TFormCadastroGondolas = class(TForm)
    EditNome: TEdit;
    Label1: TLabel;
    BitBtnAdicionar: TBitBtn;
    BitBtnEditar: TBitBtn;
    BitBtnExcluir: TBitBtn;
    DBGrid1: TDBGrid;
    TableControle: TTable;
    TableControleCAMPO: TStringField;
    TableControleVALOR: TFloatField;
    Table1: TTable;
    DataSource1: TDataSource;
    Table1ID_GONDOLA: TStringField;
    Table1NOME: TStringField;
    BitBtnSalvar: TBitBtn;
    BitBtnCancelar: TBitBtn;
    procedure BitBtnAdicionarClick(Sender: TObject);
    procedure BitBtnExcluirClick(Sender: TObject);
    procedure BitBtnEditarClick(Sender: TObject);
    procedure BitBtnSalvarClick(Sender: TObject);
    procedure BitBtnCancelarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormCadastroGondolas: TFormCadastroGondolas;

implementation

{$R *.dfm}

procedure TFormCadastroGondolas.BitBtnAdicionarClick(Sender: TObject);
var
id_gondola : integer;
begin
  if EditNome.Text <> EmptyStr then
  begin
    TableControle.Close;
    TableControle.Open;
    TableControle.Filtered := false;
    TableControle.Filter := 'CAMPO = '+QuotedStr('ID_GONDOLA');
    TableControle.Filtered := true;
    id_gondola := TableControleVALOR.AsInteger + 1;
    Table1.Close;
    Table1.Open;
    Table1.Append;
    Table1ID_GONDOLA.AsInteger := id_gondola;
    Table1NOME.AsString := EditNome.Text;
    Table1.Post;
    TableControle.Edit;
    TableControleVALOR.AsInteger := id_gondola;
    TableControle.Post;
    EditNome.Text := '';
  end;
end;

procedure TFormCadastroGondolas.BitBtnCancelarClick(Sender: TObject);
begin
  BitBtnEditar.Visible := true;
  BitBtnAdicionar.Visible := true;
  BitBtnExcluir.Visible := true;

  BitBtnSalvar.Visible := false;
  BitBtnCancelar.Visible := false;

  DBGrid1.Enabled := true;

  EditNome.Text := '';
end;

procedure TFormCadastroGondolas.BitBtnEditarClick(Sender: TObject);
begin
  EditNome.Text := Table1NOME.AsString;
  BitBtnEditar.Visible := false;
  BitBtnAdicionar.Visible := false;
  BitBtnExcluir.Visible := false;

  BitBtnSalvar.Visible := true;
  BitBtnCancelar.Visible := true;

  DBGrid1.Enabled := false;

end;

procedure TFormCadastroGondolas.BitBtnExcluirClick(Sender: TObject);
begin
  Table1.Edit;
  if MessageDlg('Tem certeza que deseja excluir a gondola '+Table1NOME.AsString+' ?',mtConfirmation,[mbYes,mbNo],0) = mrYes then
  begin
    Table1.Delete;
  end;

end;

procedure TFormCadastroGondolas.BitBtnSalvarClick(Sender: TObject);
begin
  Table1.Edit;
  Table1NOME.AsString := EditNome.Text;
  Table1.Post;

  BitBtnEditar.Visible := true;
  BitBtnAdicionar.Visible := true;
  BitBtnExcluir.Visible := true;

  BitBtnSalvar.Visible := false;
  BitBtnCancelar.Visible := false;

  DBGrid1.Enabled := true;

  EditNome.Text := '';

end;

end.
